////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Michiel Helvensteijn                                    //
//                                                                            //
// This file is part of the Mist compiler.                                    //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// along with the Mist compiler.  If not, see <http://www.gnu.org/licenses/>. //
////////////////////////////////////////////////////////////////////////////////

#include "ast/arithmeticoperation.hpp"

/******************************************************************************
 * Includes                                                                   *
 ******************************************************************************/

#include <boost/shared_ptr.hpp>
using boost::dynamic_pointer_cast;

#include "assert.hpp"

/******************************************************************************
 * Function Implementations                                                   *
 ******************************************************************************/

string to_string(ArithmeticOperator operator_type) {
	switch (operator_type) {
		case BinaryPlusOperator:     return "+";
		case BinaryMinusOperator:    return "-";
		case MultiplicationOperator: return "*";
		case DivisionOperator:       return "/";
		case ModuloOperator:         return "mod";
		default: assert(false);
	}
}

shared_ptr<ArithmeticOperation> ArithmeticOperation::create(shared_ptr<Expression> leftOperand, ArithmeticOperator operatorType, shared_ptr<Expression> rightOperand) {
	return shared_ptr<ArithmeticOperation>(new ArithmeticOperation(leftOperand, operatorType, rightOperand));
}

ArithmeticOperation::ArithmeticOperation(shared_ptr<Expression> leftOperand, ArithmeticOperator operatorType, shared_ptr<Expression> rightOperand)
	: _left_operand(leftOperand), _operator_type(operatorType), _right_operand(rightOperand) {
	assert( leftOperand);
	assert(rightOperand);
}

bool higher_precedence(ArithmeticOperator a, ArithmeticOperator b) {
	return (b <= 2 && 3 <= a);
}

bool ArithmeticOperation::equals(shared_ptr<Node> other) {
	assert(other);
	
	return typeid(*other) == typeid(ArithmeticOperation)
	    && operator_type() ==  dynamic_pointer_cast<ArithmeticOperation>(other)->operator_type()
	    && left_operand()->equals(dynamic_pointer_cast<ArithmeticOperation>(other)->left_operand())
	    && right_operand()->equals(dynamic_pointer_cast<ArithmeticOperation>(other)->right_operand());
}
